package zrweb;

import java.io.DataOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Logout
{
	private Connection connect = null;
	
	public void getLogout(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		// get the username and password
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		// connect to database to update wetherOnService column and clear locations
		ConnDB conndb = new ConnDB();
		
		// NOTE: string -> varchar for posrgresql query by using ''
		String updateSQL = "update userinfor set onservice = FALSE where username='" + username + "' and passwords='" + password + "'";
		// give feedback on whether can login or not for Android mobile
		DataOutputStream dos = new DataOutputStream(response.getOutputStream());
		dos.writeUTF("Succeed");
		try
		{
			connect = conndb.getConn();
			Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); 
			stmt.executeUpdate(updateSQL);		  
            stmt.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			ConnDB.releaseConnection(connect);
		}
	}
}